package Server;

import com.me.websocket.WsServer;
import org.java_websocket.WebSocketImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Server {
    public static final int WS_PORT = 8887;
    //    public final static int DB_PORT = 2233;
    private static int status = 0;
    public static final int TIMEPERIOD = 3;// 多久更新一次账单
    public static final double FEE = 1.0;// 每分钟耗电
    // 0 超级管理员登出 1 超级管理员登录
    private static final SuperAdmin superAdmin = new SuperAdmin("root", "root");

    public static void main(String[] args) throws IOException {
        WebSocketImpl.DEBUG = false;
        WsServer s = new WsServer(WS_PORT);
        s.start();
        System.out.println("Server running...");

        ScheduledExecutorService service = Executors.newScheduledThreadPool(10);

        long initialDelay = 0;
        ScheduledExecutor scheduledExecutor = new ScheduledExecutor();
        // 从现在开始，每隔1min执行一次更新账单
        service.scheduleAtFixedRate(scheduledExecutor, initialDelay, TIMEPERIOD, TimeUnit.SECONDS);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
        String str;
        while (true/*!(str = bufferedReader.readLine()).equals("exit")*/) {
            str = bufferedReader.readLine();
            String[] tokens = str.split(" ");
            String command = tokens[0];
            if (status == 0) {//超级管理员未登录
                if ("login".equals(command)) {
                    if (tokens.length == 3) {
                        if (superAdmin.rootLogin(tokens[1], tokens[2])) {
                            status = 1;
                            System.out.println("ROOT LOGIN SUCCESS");
                        }
                        else {
                            System.out.println("ERROR,CHECK YOUR NAME AND PASSWORD");
                        }
                    }
                    else {
                        System.out.println("WRONG PARAMETER，LOGIN COMMAND：login id password");
                    }
                }
                else {
                    System.out.println("PLEASE LOGIN");
                }
            }
            else {
                switch (command) {
                    case "create":
                        if (tokens.length == 4) {
                            if (superAdmin.create(tokens[2], tokens[3], tokens[1])) {
                                System.out.println(tokens[3] + ":" + tokens[1] + " CREATE SUCCESS");
                            }
                            else {
                                System.out.println("ERROR, CREATE COMMAND: create user_auth user_id user_password ");
                            }
                        }
                        else {
                            System.out.println("WRONG PARAMETER，CREATE COMMAND:create user_auth user_id user_password ");
                        }
                        break;
                    case "delete":
                        if (tokens.length == 2) {
                            if (superAdmin.userDelete(tokens[1])) {
                                System.out.println(tokens[1] + " DELETE SUCCESS");
                            }
                            else {
                                System.out.println("ERROR, DELETE COMMAND: delete user_id");
                            }
                        }
                        else {
                            System.out.println("WRONG PARAMETER，DELETE COMMAND: delete user_id ");
                        }
                        break;
                    case "logout":
                        if (tokens.length == 1) {
                            status = 0;
                            System.out.println("ROOT LOGOUT SUCCESS");
                        }
                        else {
                            System.out.println("WRONG PARAMETER，LOGOUT COMMAND: logout");
                        }
                        break;
                    default:
                        System.out.println("WRONG COMMAND");
                        break;

                }
            }
        }
    }
}

